<!-- 公司信息 -->
<template>
  <div class="company_information">
    <div v-if="readOnly">
      <div class="ci_title">
        公司信息
        <el-button v-if="mainShopLevel" @click="edit" type="primary" text>编辑</el-button>
      </div>
      <div v-if="mainShopLevel" class="ci_info">
        <div class="ci_info_item">
          <span class="ci_info_item_title">公司名称</span>
          <span>{{ data.name }}</span>
        </div>
        <div class="ci_info_item">
          <span class="ci_info_item_title">公司logo</span>
          <el-image :src="data.logo" fit="fill" />
        </div>
        <div class="ci_info_item">
          <span class="ci_info_item_title">客服邮箱</span>
          <span>{{ data.mail }}</span>
        </div>
      </div>
      <div v-else class="ci_info">
        <div class="ci_info_item">
          <span class="ci_info_item_title">基地名称</span>
          <span>{{ data.shopName }}</span>
        </div>
      </div>
    </div>
    <div v-else>
      <div class="ci_title">编辑公司信息</div>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px" class="ci_info base_details_form">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="公司logo" prop="logo">
          <UploadSinglePicture v-model="form.logo" :width="80" />
        </el-form-item>
        <el-form-item label="客服邮箱" prop="name">
          <el-input v-model="form.mail" placeholder="请输入" />
        </el-form-item>
        <div class="ci_info_footer">
          <el-button @click="submitForm" type="primary">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import UploadSinglePicture from "@com/upload/UploadSinglePicture.vue";
import { Edit } from "@element-plus/icons-vue";
import $ from "@util/base"
import api from "@api/company";
import { ref, computed } from "vue";
import useAccoutStore from "@store/accout"

const accoutStore = useAccoutStore()

const mainShopLevel = computed(() => {
    return accoutStore.userInfo.mainStore
})

const readOnly = ref(true);

const data = ref({})

const formRef = ref("");

const form = ref({
  name: "",
  logo: "",
  mail: "",
  shopName: ""
});

const rules = {
  name: [
    {
      required: true,
      message: "请输入公司名称",
      trigger: "blur",
    },
  ],
  logo: [
    {
      required: true,
      message: "请上传公司logo",
      trigger: "blur",
    },
  ],
  mail: [
    {
      required: true,
      message: "请输入客服邮箱",
      trigger: "blur",
    },
    {
      type: 'email',
      message: '请输入正确的邮箱地址',
      trigger: ['blur', 'change'],
    }
  ],
};

getCompany();
function getCompany() {
  api.getCompany((val) => {
    data.value = $.deepCopy(val)
  });
}

function edit() {
  readOnly.value = false;
  form.value = $.deepCopy(data.value)
}

function submitForm() {
  formRef.value.validate((valid, fields) => {
    if (valid) {
      api.updateCompany(form.value, (val) => {
        cancel()
      });
    } else {
      return false
    }
  })
}

function cancel() {
  readOnly.value = true;
  getCompany()
}
</script>

<style lang="scss">
.company_information {
  padding: 24px;

  .ci_title {
    font-size: 16px;
    color: #222;
  }

  .ci_info {
    margin-top: 40px;
    margin-left: 80px;

    .ci_info_item {
      display: flex;
      align-items: center;
      margin-bottom: 20px;

      .ci_info_item_title {
        margin-right: 20px;
      }

      .el-image {
        height: 80px;
        width: 80px;

        .el-image__error,
        .el-image__inner,
        .el-image__placeholder,
        .el-image__wrapper,
        img {
          border-radius: 50%;
        }
      }
    }

    .ci_info_footer {
      padding-left: 80px;

      .el-button:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
